<template>
  <div class="info-dialog">
    <el-dialog
      :visible.sync="visible"
      :title="title"
      :modal-append-to-body="true"
      :append-to-body="true"
      :destroy-on-close="false"
      :modal="true"
      :close-on-click-modal="false"
      top="10vh"
      width="550px"
      @closed="closed"
    >
      <el-form ref="formData" :model="formData" :rules="rules">
        <el-form-item label-width="100px" label="账号" prop="username">
          <el-input v-model="formData.username" placeholder="请输入账号" autocomplete="off" />
        </el-form-item>
        <el-form-item label-width="100px" label="手机号码" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入手机号码" autocomplete="off" />
        </el-form-item>

        <el-form-item label-width="100px" label="密码" prop="password">
          <el-input v-model="formData.password" placeholder="请输入密码" autocomplete="off"  show-password/>
        </el-form-item>

        <el-form-item label-width="100px" label="确认密码" prop="password2">
          <el-input v-model="formData.password2" placeholder="请再次输入密码" autocomplete="off"  show-password/>
        </el-form-item>


      </el-form>

      <div style="text-align: right;">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


  import {
    saveUser,
    getUserDeptIdsById,
    getUserRoleIdsById
  } from '@/api/system/user'

  import {
    getDeptOptionsTree
  } from '@/api/system/dept'

  import {
    getRoleOptions
  } from '@/api/system/role'

  export default {
    name: 'RegDialog',
    components: {
    },
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed:{

    },
    watch:{
      properties: {
        handler: function(value) {
          this.visible = value.visible
          this.formData.username = ''
          this.formData.phone = ''
          this.formData.password = ''
          this.formData.password2 = ''
        },
        immediate: true,
        deep: false
      }
    },
    data(){

      const validatePassword2 = (rule, value, callback) => {
        if (!Object.is(this.formData.password,value)) {
          callback(new Error('密码不一致！'))
        } else {
          callback()
        }
      }

      return {
        visible:false,
        title:'注册账号',
        loading:false,
        formData: {
          username: '',
          phone: '',
          password:'',
          password2:''
        },
        rules: {
          username: [{
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 50,
            message: '长度在 2 到 50 个字符',
            trigger: 'blur'
          }
          ],
          phone: [{
              required: true,
              message: '请输入手机号码',
              trigger: 'blur'
            },
            {
              min: 11,
              max: 50,
              message: '长度在 11 到 50 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 11,
              max: 50,
              message: '长度在至少6位',
              trigger: 'blur'
            }
          ],
          password2: [{
              required: true,
              trigger: 'blur',
              validator: validatePassword2
            }
          ]
        }
      }
    },
    created() {
    },
    methods:{
      submitForm: function() {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            this.loading = true
            saveRole(this.formData).then(rsp => {
              this.$message({
                type: 'success',
                message:rsp.message
              })
              this.closed()
            }).catch(err => {
              console.log(err);
            })
            .finally(() => {
              this.loading = false
            })
          }
        })
      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }

</script>

<style>
</style>
